<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="addPermission({type:1,pid:0})">添加权限</el-button>
        </div>
        <el-table :data="list" row-key="id" border>
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button type="text" @click="addPermission({type:2,pid:row.id})">添加</el-button>
              <el-button type="text" @click="editPermission(row.id)">编辑</el-button>
              <el-button type="text" @click="delPermission(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <el-dialog :visible="showDialog" title="弹层标题" @close="closeDialog">
      <el-form ref="dataForm" label-width="100px" :model="formData" :rules="rules">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { transTree } from '@/utils/transTree'
export default {
  name: 'Permission',
  data() {
    const checkName = async(rule, value, callback) => {
      const res = await this.$http.get('/sys/permission')
      const arrList = []
      if (this.formData.id) {
        res.forEach(item => {
          if (item.name !== this.editName) {
            arrList.push(item.name)
          }
        })
      } else {
        res.forEach(item => {
          arrList.push(item.name)
        })
      }
      if (arrList.includes(value)) {
        callback(new Error('权限名称已被占用'))
      } else {
        callback()
      }
    }
    const checkCode = async(rule, value, callback) => {
      const res = await this.$http.get('/sys/permission')
      const arrList = []
      if (this.formData.id) {
        res.forEach(item => {
          if (item.code !== this.editCode) {
            arrList.push(item.code)
          }
        })
      } else {
        res.forEach(item => {
          arrList.push(item.code)
        })
      }
      if (arrList.includes(value)) {
        callback(new Error('权限标识已被占用'))
      } else {
        callback()
      }
    }
    return {
      list: [],
      editName: '',
      editCode: '',
      showDialog: false,
      formData: {
        name: '',
        code: '',
        description: '',
        type: null,
        pid: '',
        enVisible: ''
      },
      rules: {
        name: [
          { required: true, message: '权限名称必填', trigger: 'blur' },
          { validator: checkName, trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '权限标识必填', trigger: 'blur' },
          { validator: checkCode, trigger: ['blur', 'change'] }

        ],
        description: [
          { required: true, message: '权限介绍必填', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await this.$http.get('/sys/permission')
      this.list = transTree(res)
    },
    resetForm() {
      this.$refs.dataForm.resetFields()
      this.formData = {
        name: '',
        code: '',
        description: '',
        type: null,
        pid: '',
        enVisible: '',
        id: ''
      }
      this.showDialog = false
    },
    closeDialog() {
      this.resetForm()
    },
    addPermission({ type, pid }) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    confirm() {
      this.$refs.dataForm.validate(async(valid) => {
        if (!valid) return
        if (this.formData.id) {
          await this.$http.put(`/sys/permission/${this.formData.id}`, this.formData)
        } else {
          await this.$http.post('/sys/permission', this.formData)
        }
        this.$message.success(`${this.formData.id ? '修改' : '添加'}权限成功`)
        this.resetForm()
        this.getList()
      })
    },
    async editPermission(id) {
      this.showDialog = true
      const res = await this.$http.get(`/sys/permission/${id}`)
      this.formData = res
      this.editName = res.name
      this.editCode = res.code
    },
    delPermission(id) {
      this.$confirm('确认删除此权限吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await this.$http.delete(`/sys/permission/${id}`, id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(() => {

      })
    }
  }
}
</script>

<style>
</style>
